/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.notify.service;

import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.service.CrudService;
import com.thinkgem.jeesite.common.utils.IdGen;
import com.thinkgem.jeesite.modules.notify.dao.NotiAppDao;
import com.thinkgem.jeesite.modules.notify.entity.NotiApp;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 推送app管理Service
 *
 * @author hf
 * @version 2018-12-17
 */
@Service
@Transactional(readOnly = true)
public class NotiAppService extends CrudService<NotiAppDao, NotiApp> {

    @Autowired
    private NotiAppDao notiAppDao;

    public NotiApp get(String id) {
        return super.get(id);
    }

    public List<NotiApp> findList(NotiApp notiApp) {
        return super.findList(notiApp);
    }

    public Page<NotiApp> findPage(Page<NotiApp> page, NotiApp notiApp) {
        //当前登录人只能看到自己的app
        User user = UserUtils.getUser();
        if (StringUtils.isNotBlank(user.getId())) {
            notiApp.setSysUserId(user.getId());
        }
        /*//处理图片路劲问题
		Page<NotiApp> pages = super.findPage(page, notiApp);
		List<NotiApp> list = pages.getList();
		*/
        return super.findPage(page, notiApp);
    }

    @Transactional(readOnly = false)
    public void save(NotiApp notiApp) {
        //处理appkey、secret、sys_user_id
        User user = UserUtils.getUser();
        if (StringUtils.isNotBlank(user.getId())) {
            notiApp.setSysUserId(user.getId());
        }
        if (notiApp.getIsNewRecord()) {
            notiApp.setAppKey(IdGen.uuid());
            notiApp.setAppSecret(IdGen.uuid());
        }
        //处理图片路径问题
        String appIcon = notiApp.getAppIcon();
        if (appIcon.contains("|")) {
            notiApp.setAppIcon(appIcon.substring(1));
        }
        super.save(notiApp);
    }

    @Transactional(readOnly = false)
    public void delete(NotiApp notiApp) {
        super.delete(notiApp);
    }

    public List<String> getAppid() {
        List<String> appids = notiAppDao.getAppid();
        if (appids.size() <= 0) {
            return new ArrayList<String>();
        }
        return appids;
    }

}